/**
 * 标准内容编辑
 * @author linfeng
 * @since 2020/02/13
 */
var form = null;

layui.define(['form', 'layedit', 'laydate', 'upload'], function (exports) {
    let $ = layui.$;
    form = layui.form;
    let upload = layui.upload;
    let layer = layui.layer;
    let index;

    upload.render({
        elem: '#upload'
        ,url: '/common/uploadFile.action' //改成您自己的上传接口
        ,data: {folderName: 'standardRecord'}
        ,accept: 'file' //普通文件
        ,multiple: false
        ,size:1024*25
        ,exts: 'jpg|png|jpeg|xlsx|xls|csv|pdf|ppt|pptx|doc|docx|rar|zip|7z'
        ,done: function(res){
            var uploadFiles = document.getElementsByClassName('uploadFile');
            var size = uploadFiles.length;

            if (size >= 5){
                layer.alert('添加失败，附件最多不超过五个！');
                return false;
            }

            let fileSrc = $('#fileSrc').attr('href');
            let fileSrc2 = $('#fileSrc2').attr('href');
            let fileSrc3 = $('#fileSrc3').attr('href');
            let fileSrc4 = $('#fileSrc4').attr('href');
            let fileSrc5 = $('#fileSrc5').attr('href');

            var addName = "";
            if (size == 0) {
                addName = "";
            } else {
                // addName = size + 1;
                if (fileSrc == undefined) {
                    addName = "";
                } else if (fileSrc2 == undefined) {
                    addName = 2;
                } else if (fileSrc3 == undefined) {
                    addName = 3;
                } else if (fileSrc4 == undefined) {
                    addName = 4;
                } else if (fileSrc5 == undefined) {
                    addName = 5;
                }
            }

            var fileSize = 0;
            fileSize = res.data.size * 1.0 / 1024;
            fileSize = fileSize.toFixed(2);
            fileSize = fileSize + 'KB';

            $("#files").append('<div id="file">\n' +
                '                            <div style="margin-left: 70px;" class="layui-input-block">\n' +
                '                                <div class="layui-input-inline" style="width: auto">\n' +
                '                                    <a id="fileSrc'+ addName +'" href="' + res.data.src + '" class="layui-form-label uploadFile" style="width: auto;">' + res.data.name + '</a>\n' +
                '                                    <span id="fileSize'+ addName +'" value="'+ res.data.size +'" class="layui-form-label" style="width: auto; color: #9F9F9F;">' + fileSize + '</span>\n' +
                '                                    <a class="layui-form-label" onclick="deleteFile(\'fileSrc'+ addName +'\')" style="width:auto;color: #00a0e9">\n' +
                '                                        删除\n' +
                '                                    </a>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="layui-split">&nbsp;</div>\n' +
                '                        </div>');

            form.render();
            layer.msg('上传成功');
            console.log(res);
        }
    });

    form.render(null, "zmn-form");

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;
        let url = "/standard/record/addEditContent.action";
        data['fileSrc'] = $('#fileSrc').attr('href');
        data['fileSrc2'] = $('#fileSrc2').attr('href');
        data['fileSrc3'] = $('#fileSrc3').attr('href');
        data['fileSrc4'] = $('#fileSrc4').attr('href');
        data['fileSrc5'] = $('#fileSrc5').attr('href');
        data['fileSize'] = $('#fileSize').attr('value');
        data['fileSize2'] = $('#fileSize2').attr('value');
        data['fileSize3'] = $('#fileSize3').attr('value');
        data['fileSize4'] = $('#fileSize4').attr('value');
        data['fileSize5'] = $('#fileSize5').attr('value');

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                layer.close(index);
                layer.msg(json.message);
                if (json.status == 200) {
                    setTimeout(function () {
                        window.location.href="/standard/record/permission.action?standardId="+json.data;
                    },1000);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete:function () {
                layer.close(index);
            }
        })

        return false;
    });

    // 提交
    form.on('submit(zmn-form-preView-submit)', function (obj) {
        let data = obj.field;
        let url = "/standard/record/addEditContent.action";
        data['fileSrc'] = $('#fileSrc').attr('href');
        data['fileSrc2'] = $('#fileSrc2').attr('href');
        data['fileSrc3'] = $('#fileSrc3').attr('href');
        data['fileSrc4'] = $('#fileSrc4').attr('href');
        data['fileSrc5'] = $('#fileSrc5').attr('href');
        data['fileSize'] = $('#fileSize').attr('value');
        data['fileSize2'] = $('#fileSize2').attr('value');
        data['fileSize3'] = $('#fileSize3').attr('value');
        data['fileSize4'] = $('#fileSize4').attr('value');
        data['fileSize5'] = $('#fileSize5').attr('value');
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                layer.close(index);
                layer.msg(json.message);
                if (json.status == 200) {
                    setTimeout(function () {
                        var url = "/standard/record/preview.action?standardId="+json.data+"&flag="+1;
                        zmnLayUtil.openTabsPageCustom(parent, url, "预览");
                    },1000);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete:function () {
                layer.close(index);
            }
        })

        return false;
    });

    //对外暴露的接口
    exports('standard_record_content_edit_js', {});
});

function deleteFile(element) {
    var compoent = document.getElementById(element);
    var block = compoent.parentNode.parentNode;
    var file = compoent.parentNode.parentNode.parentNode;
    var files = compoent.parentNode.parentNode.parentNode.parentNode;
    files.removeChild(file);
    form.render();
}


